#include "videowindow.h"
#include <QMessageBox>


VideoWindow::VideoWindow(QString cameraName, QString intf, QString ip, quint16 listenPort, int bindWay, QWidget *parent) : QMainWindow(parent),
    m_cameraName(cameraName),
    m_listenPort(listenPort),
    m_intf(intf),
    m_ip(ip),
    m_bindWay(bindWay)
{
    // 创建中央部件和布局
    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);
    QVBoxLayout *receiverLayout = new QVBoxLayout(centralWidget);

    //视频显示窗口
    m_player = new VideoPlayer(this);
    receiverLayout->addWidget(m_player);

    //连接接收信号和显示槽
    m_receive = new VideoReceiver(m_player, m_listenPort, m_ip, m_intf, m_bindWay);
    connect(m_receive, &VideoReceiver::frameReceived, this, &VideoWindow::onFrameReceived);
    connect(m_receive, &VideoReceiver::sigSendDataToSave, this, &VideoWindow::sigSendDataToSave); //继续转存
    connect(m_receive, &VideoReceiver::sigFrameAvailable, [=](const QImage &image){
        m_player->displayFrame(image);
    });
    connect(m_receive, &VideoReceiver::sigSrcIP, this, &VideoWindow::sigSrcIP);
    connect(m_receive, &VideoReceiver::sigVideoInfoChanged, this, &VideoWindow::sigVideoInfoChanged);

}

void VideoWindow::startReceiving()
{
    m_receive->init();
    QThread::msleep(5); //give a init time, JianYu
    m_receive->startReceiving();
    //m_receive->stopReceiving();
}

void VideoWindow::resetSettings(QString ip, quint16 listenPort, QString infName, int bindWay)
{
    m_ip = ip;
    m_listenPort = listenPort;
    m_receive->resetSettings(m_ip, m_listenPort, infName, bindWay);
}

void VideoWindow::onFrameReceived(const QImage &image)
{
    qDebug() << "QImage is :" << image.isNull();
    m_player->displayFrame(image);

    // 保存
    // QString timeStr = QDateTime::currentDateTime().toString("yyMMdd_hh_mm_ss:") + QString::number(countTime) + ".png";
    // qDebug() << "file:" << timeStr;
    //image.save(QString("img/%1").arg(timeStr));
    //m_videoLabels->setPixmap(QPixmap::fromImage(image).scaled(m_videoLabels->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

}
